package com.lanou.tipsy.fragment.songsList;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.android.volley.Response;
import com.facebook.drawee.view.SimpleDraweeView;
import com.lanou.tipsy.R;
import com.lanou.tipsy.adapter.songList.SongsListsContentAdpater;
import com.lanou.tipsy.base.BaseFragment;
import com.lanou.tipsy.base.Constants;
import com.lanou.tipsy.entity.songList.SongListSpecific.SongListAll;
import com.lanou.tipsy.entity.songList.SongListSpecific.Songs;
import com.lanou.tipsy.tools.custom.StringRequestUtf;
import com.lanou.tipsy.tools.volley.ResponseErrorListener;
import com.lanou.tipsy.tools.volley.VolleySingleton;

import java.util.ArrayList;

/**
 * Created by dllo on 15/10/23.
 * 歌单-歌单详情
 */

public class SongsListsContentFragment extends BaseFragment implements View.OnClickListener {
    //private CollapsingToolbarLayout ctl_toolbar;
    public static final String TAG = "SongsListsContentFragment";
    private RecyclerView recyclerView;//列表
    private SongsListsContentAdpater adapter;//适配器
    private ArrayList<Songs> data;
    private Songs songs;
    private VolleySingleton volleySingleton;
    private ResponseErrorListener errorListener;
    //private ImageView song_class_iv;
    private String songid,title;//歌曲ID  标题
    private Toolbar toolbar;
    private Context context;
    private SimpleDraweeView draw_song_list_content;  //顶部图片
    private TextView songsNumTv, songsTagTv;//歌曲数量,tag
    private CollapsingToolbarLayout ctl_song_list_songs; //标题
    private String spurl;



    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fr_songs_lists_content, null);
        return view;
    }

    private void initView(View view) {
        recyclerView = (RecyclerView) view.findViewById(R.id.rv_song_list_content);
        draw_song_list_content = (SimpleDraweeView) view.findViewById(R.id.draw_song_list_content);

        ctl_song_list_songs = (CollapsingToolbarLayout) view.findViewById(R.id.ctl_song_list_songs);
        songsNumTv = (TextView) view.findViewById(R.id.tv_song_list_content_count);
        songsTagTv = (TextView) view.findViewById(R.id.tv_song_list_content_tag);
        recyclerView.setAdapter(adapter);
        //初始化toolbar  设置图片
        toolbar = (Toolbar) view.findViewById(R.id.tl_song_list_songs);
        toolbar.setNavigationIcon(R.mipmap.btn_web_back_normal);
        toolbar.setNavigationOnClickListener(this);

        volleySingleton = VolleySingleton.getInstance();
        errorListener = new ResponseErrorListener();
    }

/*
    private void click() {
        adapter.setOnItemClickLitener(new SongsListsContentAdpater.OnItemClickLitener() {
            @Override
            public void onItemClick(View view, int position) {

                songs = data.get(position);
                spurl = songs.getUrlList().get(position).getUrl();
                Log.i("验证",spurl);
                //Intent intent = new Intent(context, SpectrumActivity.class);
                //intent.putExtra("spurl","55555");
                //startActivity(intent);
                //Toast.makeText(context,
                        //position + " click, 名字是" + "",
                        //Toast.LENGTH_SHORT).show();
            }
        });
    }*/

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView(getView());
        Bundle bundle = getArguments();
        songid = (String) bundle.get("songid");
        title = (String) bundle.get("title");
        toolbar.setTitle(title);
        Log.i("跳转后", songid);
        initNetWork();
    }

    /***
     * 网络拉取
     */
    private void initNetWork() {
        String url = Constants.SONGS_LIST_DETAIL_START +
                songid + Constants.SONGS_LIST_DETAIL_END;
        Log.i("获取url",url);
        //防止解析的数据乱码
        StringRequestUtf sru = new StringRequestUtf(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                if (response != null) {
                    String str = response.toString();
                    SongListAll songListAll = JSON.parseObject(str, SongListAll.class);
                    data = songListAll.getSongs();
                    adapter = new SongsListsContentAdpater(getActivity(), data);
                    recyclerView.setAdapter(adapter);
                    recyclerView.setLayoutManager(new LinearLayoutManager(context));
                    recyclerView.setItemAnimator(new DefaultItemAnimator());
                    songsNumTv.setText("共" + data.size() + "首歌");
                    songsTagTv.setText(""+songListAll.getDesc());
                    Uri uri = Uri.parse(songListAll.getImage().getPic());
                    draw_song_list_content.setImageURI(uri);
                    ctl_song_list_songs.setTitle(""+songListAll.getTitle());
                }
            }
        }, errorListener);
        volleySingleton.addQueue(sru, TAG);
    }


    @Override
    public void onClick(View v) {
        getActivity().getSupportFragmentManager().popBackStack();
    }
    @Override
    public void onStop() {
        super.onStop();
        volleySingleton.removeRequest(TAG);

    }
}
